<?php

//接入微信
// $s = $_GET['signature'];
//
// $t = $_GET['timestamp'];
//
// $n = $_GET['nonce'];
//
// $e = $_GET['echostr'];
//
// $token = 'hongwei';
//
// $tmpArr = array($t,$n,$token);
//
// sort($tmpArr,SORT_STRING);
//
// $str = implode($tmpArr);
//
// $sha = sha1($str);
//
// if($sha == $s){
// 	echo $e;
// }
// exit;

//接受微信服务器消息
$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];
$p = simplexml_load_string($postStr); //将微信服务器消息转化为对象

$userid = $p->FromUserName;  //用户ID
$touser = $p->ToUserName;  //我的用户名
$msgtype = $p->MsgType;  //用户发过来的消息类型
$cont = $p->Content;  //文本消息内容
$m = $p->MsgId; //消息ID

//file_put_contents('./a.txt',$cont);
//回复文本信息
$str_spl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";

//回复音乐消息
$mus_spl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
</xml>";

//回复图文消息5
$item_spl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";

$vid_spl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[%s]]></MediaId>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video>
</xml>";
// 9Tu31sPTzB52KHCxewK6TBThQORW1xUhiax8qWxMnncWDd8zAugIctOAjOSIKYTjIJqlKO9dNyqDcnJal5c9-zWc1bwubds1mharWHyyrfkOp5zEDG7WDRBinmv8LKE-JFJgABAOKO
//$touser = 'fb1217867491';
$time = time();
$text = 'text';

if($msgtype == 'text'){
    if($cont == 2){
        $data = '你好, 傻X!!!';
        $backstr = sprintf($str_spl,$userid,$touser,$time,$text,$data);
        echo $backstr;
    }elseif($cont == 'music'){
        $mt = 'Alan Walker';
        $mc = 'Alan Walker - Fade - 纯音乐版';
        $mu = 'http://fbhongwei.ittun.com/chun.mp3';
        $mhu = 'http://fbhongwei.ittun.com/chun.mp3';
        $backmusic = sprintf($mus_spl,$userid,$touser,$time,$mt,$mc,$mu,$mhu);
        echo $backmusic;

    }elseif($cont == 'pic'){
        $title = '美女';
        $desc = '美美的';
        $url = 'http://fbhongwei.ittun.com/pretty.jpg';
        $tu = 'http://image.baidu.com/search/detail?z=0&ipn=false&word=angelababy&step_word=&hs=0&pn=3&spn=0&di=74773101150&pi=&tn=baiduimagedetail&is=0%2C0&istype=&ie=utf-8&oe=utf-8&cs=4005154672%2C893719335&os=4216139659%2C1564540096&simid=&adpicid=0&fm=&sme=&cg=star&bdtype=0&simics=1820692077%2C98981263&oriquery=&objurl=http%3A%2F%2Fimg.9ku.com%2Fgeshoutuji%2Fsingertuji%2F4%2F40710%2F40710_6.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Blh7_z%26e3Bv54AzdH3Fftg2j6t42AzdH3F9a08aAzdH3Fddbcm&gsm=0&cardserver=1';
        $backtw = sprintf($item_spl,$userid,$touser,$time,$title,$desc,$url,$tu);
        echo $backtw;
    }else{
        $data = "回复music 有你想听的音乐\n 回复pic 有你想看的美图\n 回复2 有你想看的视频";
        $backstr = sprintf($str_spl,$userid,$touser,$time,$text,$data);
        echo $backstr;
    }
}else if($msgtype == 'event'){
  if($p->Event == 'CLICK'){
    if($p->EventKey == 'music'){
      $mt = 'Alan Walker';
      $mc = 'Alan Walker - Fade - 纯音乐版';
      $mu = 'http://fbhongwei.ittun.com/chun.mp3';
      $mhu = 'http://fbhongwei.ittun.com/chun.mp3';
      $backmusic = sprintf($mus_spl,$userid,$touser,$time,$mt,$mc,$mu,$mhu);
      echo $backmusic;
    }elseif($p->EventKey == 'pic'){
      $title = '美女';
      $desc = '美美的';
      $url = 'http://fbhongwei.ittun.com/angel.jpeg';
      $tu = 'http://image.baidu.com/search/detail?z=0&ipn=false&word=angelababy&step_word=&hs=0&pn=3&spn=0&di=74773101150&pi=&tn=baiduimagedetail&is=0%2C0&istype=&ie=utf-8&oe=utf-8&cs=4005154672%2C893719335&os=4216139659%2C1564540096&simid=&adpicid=0&fm=&sme=&cg=star&bdtype=0&simics=1820692077%2C98981263&oriquery=&objurl=http%3A%2F%2Fimg.9ku.com%2Fgeshoutuji%2Fsingertuji%2F4%2F40710%2F40710_6.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Blh7_z%26e3Bv54AzdH3Fftg2j6t42AzdH3F9a08aAzdH3Fddbcm&gsm=0&cardserver=1';
      $backtw = sprintf($item_spl,$userid,$touser,$time,$title,$desc,$url,$tu);
      echo $backtw;
    }
  }
}else if($msgtype == 'image'){
  $url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
  $imgurl = $p->PicUrl;
  $data = [
    'api_key' => 'PUrG_GXz0oRnnoQ4K_xtonnQnK2IrStX',
    'api_secret' => 'RT4727qL8sTmcmk0Y1jWnPsAqcWsvAEt',
    'image_url' => $imgurl,
    'return_landmark' => '1',
    'return_attributes' => 'age'
  ];
  $face = count(json_decode(gethttps($url,$data),true)['faces']);

  $data = "图片中有".$face."个人";
  $backstr = sprintf($str_spl,$userid,$touser,$time,$text,$data);
  echo $backstr;
}else if($msgtype == 'voice'){
  $data = '你的声音好好听啊... ...';
  $backstr = sprintf($str_spl,$userid,$touser,$time,$text,$data);
  echo $backstr;
}else if($msgtype == 'video'){
  $data = '视频挺好的';
  $backstr = sprintf($str_spl,$userid,$touser,$time,$text,$data);
  echo $backstr;
}else if($msgtype == 'location'){
  $x = $p->Location_X; //纬度
  $y = $p->Location_Y; //精度

  //URL中文汉字
  $url = "http://api.map.baidu.com/telematics/v3/local?location=".$y.",".$x."&keyWord=厕所&output=json&ak=GK9TuD7wVz7oIbzqlt0FRL59gTUMLB4y";
  $cesuo = file_get_contents($url);
  $arr_cesuo = json_decode($cesuo,true)['pointList'];
  // file_put_contents('./a.txt',$cesuo);
  $b = '';
  foreach($arr_cesuo as $c){
    $b .= '在'.$c['address'].'有厕所,赶紧去距离你有'.$c['distance']."米\n";
  }

  $backstr = sprintf($str_spl,$userid,$touser,$time,$text,$b);
  echo $backstr;
}

$data =  '{
   "button":[
   {
        "type":"click",
        "name":"今日歌曲",
        "key":"music"
    },
    {
         "name":"菜单",
         "sub_button":[
         {
             "type":"view",
             "name":"搜索",
             "url":"http://www.soso.com/"
          },
          {
             "type":"view",
             "name":"视频",
             "url":"http://youku.com/"
          },
          {
             "type":"click",
             "name":"美图UU",
             "key":"pic"
          }]
     }]
}';

$url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=9Tu31sPTzB52KHCxewK6TBThQORW1xUhiax8qWxMnncWDd8zAugIctOAjOSIKYTjIJqlKO9dNyqDcnJal5c9-zWc1bwubds1mharWHyyrfkOp5zEDG7WDRBinmv8LKE-JFJgABAOKO";

function gethttps($url,$data=""){

  //1.初始化链接资源
  $curl = curl_init();

  //2.设置参数
  curl_setopt($curl,CURLOPT_URL,$url);
  curl_setopt($curl,CURLOPT_HEADER,0);
  curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($crul,CURLOPT_SSL_VERIFYPEER,0);
  curl_setopt($curl,CURLOPT_POSTFIELDS,$data);

  //3.发送请求
  $da = curl_exec($curl);

  //4.关闭请求资源
  curl_close($curl);

  //file_put_contents('./a.txt',$da);
  return $da;
}
